class ProductsController < ApplicationController
  # include ProductsHelper
  helper_method :sort_column, :sort_direction
  
  # GET /products
  # GET /products.json
  def index
    # @products = Product.search(params[:search]).paginate(:per_page => 5, :page => params[:page])    
    # @products = Product.order(sort_column + ' ' + sort_direction).paginate(:per_page => 5, :page => params[:page])
    @products = Product.search(params[:search]).order(sort_column + ' ' + sort_direction).paginate(:per_page => 25, :page => params[:page])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
    @product = Product.find(params[:id])

    puts @product.total_quantity

    @quantity = get_total_quantity

    @size_types = Array.new

    @product.stocks.each do |stock|
      @size_types.push(stock.size.size_type)
    end

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/new
  # GET /products/new.json
  def new
    @product = Product.new
    @size_types = SizeType.all

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/1/edit
  def edit
    @product = Product.find(params[:id])
    @sizes = @product.sizes
    @size_types = SizeType.all
    @size_types_selected = Array.new
    @sizes.each do |size|
      puts size.size_type.id
      @size_types_selected.push(size.size_type)
    end
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(params[:product])

    @product.size_type.each do |type|
      if ! type.blank?
        SizeType.find(type).sizes.each do |size|
          @stock = Stock.new({ :size_id => size.id  })
          @product.stocks.push(@stock)
        end
      end
    end

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render json: @product, status: :created, location: @product }
      else
        format.html { render action: "new" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.json
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { flash[:notice] = 'Product was successfully updated.'
          redirect_to @product  }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end

  # GET /products
  # GET /products.json
  # def products_by_category
  # @products = Product.where(["category_id = ?", params[:id]]).all
  #
  # @products.each do |product|
  # product.quantity = total_quantity(product)
  # end
  #
  # @product = Product.new
  # @categories = Category.all
  #
  # respond_to do |format|
  # format.js  { render :layout => false }
  # format.json { render json: @products }
  # end
  # end

  # Get the total quantity of all styles
  def get_total_quantity
    @product = Product.find(params[:id])
    quantity = 0

    @product.stocks.each do |item|
      if item.quantity.is_a?(Integer)
      quantity = quantity + item.quantity
      end
    end

    return quantity
  end

  # def total_quantity( product )
  # quantity = 0
  #
  # product.styles.each do |item|
  # if item.quantity.is_a?(Integer)
  # quantity = quantity + item.quantity
  # end
  # end
  #
  # return quantity
  # end
  #
  
  private

  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : "asc"
  end

  def sort_column
    Product.column_names.include?(params[:sort]) ? params[:sort] : "item_number"
  end

end
